import 'package:get/get.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/subs/pro_progress_day/pro_progress_day_model.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/subs/pro_progress_day/state.dart';
import 'package:jinke/utils/httputils/api.dart';
import 'package:jinke/utils/httputils/resultdata.dart';
import 'package:jinke/utils/util.dart';

import '../../model/pro_detail_model/pro_detail_model.dart';

class ProProgressDayController extends GetxController {
  ProProgressDayController();
  final state = ProProgressDayState();

  _initData() {
    update(["pro_progress_day"]);
  }

  void getList() async {
    if (state.mod == null) {
      return;
    }
    ProListApi.postProDayList(id: state.mod?.id ?? '').then((value) {
      ResultData res = value;
      if (res.isSuccess) {
        List? list = res.data['content'];
        state.list = list?.map((e) => ProProgressDayModel.fromJson(e)).toList();
        state.list?.removeWhere((element) => isNilStr(element.id));
        setupData();
        _initData();
      }
    });
  }

  void setupData() {
    state.showList = [];
    state.list?.forEach((element) {
      var hasEdit = !isNilStr(element.editReportEvolveContent);
      if (hasEdit) {
        state.showList.add(ProProgressDayShowModel()
          ..date = element.reportDate
          ..time = element.editReportTime
          ..name = element.editReportUserName
          ..content = element.editReportEvolveContent
          ..files = element.files);
      }
      state.showList.add(ProProgressDayShowModel()
        ..date = hasEdit ? null : element.reportDate
        ..time = element.reportTime
        ..name = element.reportUserName
        ..content = element.reportEvolveContent
        ..files = element.files);
    });
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    getList();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
